/**
* Copyright 2013 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.wapp.report.client.presenter;

import com.nabla.wapp.client.mvp.AbstractWizardPresenter;
import com.nabla.wapp.client.mvp.IWizardDisplay;
import com.nabla.wapp.client.ui.WizardPageNavigations;
import com.nabla.wapp.report.client.ui.ParameterWizardPageUi;
import com.nabla.wapp.report.shared.parameter.ParameterGroup;
import com.nabla.wapp.report.shared.parameter.ParameterList;
import com.nabla.wapp.report.shared.parameter.ParameterValueList;
import com.nabla.wapp.shared.slot.ISlot;


public class ReportParameterDialog extends AbstractWizardPresenter<IWizardDisplay> {

	private final ParameterList		parameters;
	private final ParameterValueList	values;
	private final ISlot				onSuccess;

	public ReportParameterDialog(final IWizardDisplay ui, final ParameterList parameters, final ParameterValueList values, final ISlot onSuccess) {
		super(ui);
		this.parameters = parameters;
		this.values = values;
		this.onSuccess = onSuccess;
	}

	@Override
	public void bind() {
		super.bind();
		displayPage(0);
	}

	private void displayPage(int pageIndex) {
		final ParameterGroup reportParameters = parameters.get(pageIndex++);
        if (pageIndex == parameters.size()) {
    		displayNextPage(new ParameterWizardPageUi(reportParameters, values, WizardPageNavigations.FINISH, new ISlot() {
    			@Override
    			public void invoke() {
				    onSuccess.invoke();
				    getDisplay().hide();
    			}
    		}));
        } else {
            final Integer nextPageIndex = pageIndex;
    	    displayNextPage(new ParameterWizardPageUi(reportParameters, values, WizardPageNavigations.NEXT, new ISlot() {
    			@Override
    			public void invoke() {
                    displayPage(nextPageIndex);
    			}
    		}));
		}
	}

}
